package com.sims.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.sims.dao.ISimsGradeDao;
import com.sims.eo.SimsGrade;
import com.sims.util.HibernateSessionFactory;

public class SimsGradeDaoImp implements ISimsGradeDao {

	@Override
	public SimsGrade getSimsGradebyGradeName(String gradeName) {
		// 从数据库获取对象
		SimsGrade simsGrade = null;
		String sql = "from SimsGrade s where s.gradeName=?";
		Query query = HibernateSessionFactory.getSession().createQuery(sql);
		query.setParameter(0, gradeName);
		List grades = query.list();
		if (grades.size() != 0) {
			simsGrade = (SimsGrade) grades.get(0);
		}
		HibernateSessionFactory.closeSession();
		return simsGrade;
	}

	@Override
	public List<SimsGrade> getAllSimsGrade() {
		String sql = "from SimsGrade";
		Query query = HibernateSessionFactory.getSession().createQuery(sql);
		List<SimsGrade> list = query.list();
		HibernateSessionFactory.closeSession();
		return list;
	}

	@Override
	public boolean deletSimsGradeById(Long id) {
		boolean bflag = false;
		// 打开Session
		Session session = HibernateSessionFactory.getSession();
		// 开始一个事务
		Transaction trans = session.beginTransaction();
		try {
			// 查询删除对象
			SimsGrade grade = (SimsGrade) session.get(SimsGrade.class, id);
			// 持久化操作
			session.delete(grade);
			// 提交事务
			trans.commit();
			bflag = true;
		} catch (Exception e) {
			System.out.println("error:" + e.getMessage());
			e.printStackTrace();
			// 回滚事务
			trans.rollback();
		} finally {
			// 关闭Session
			HibernateSessionFactory.closeSession();
		}
		return bflag;
	}

	@Override
	public SimsGrade getSimsGradeById(Long id) {
		// 打开Session
		Session session = HibernateSessionFactory.getSession();
		// 查询删除对象
		SimsGrade grade = (SimsGrade) session.get(SimsGrade.class, id);
		return grade;
	}

	@Override
	public boolean updateSimsGrade(SimsGrade grade) {
		boolean bflag = false;
		// 打开Session
		Session session = HibernateSessionFactory.getSession();
		// 开始一个事务
		Transaction trans = session.beginTransaction();
		try {
			// 持久化操作
			session.update(grade);
			// 提交事务
			trans.commit();
			bflag = true;
		} catch (Exception e) {
			System.out.println("error:" + e.getMessage());
			e.printStackTrace();
			// 回滚事务
			trans.rollback();
		} finally {
			// 关闭Session
			HibernateSessionFactory.closeSession();
		}
		return bflag;
	}

	@Override
	public List<SimsGrade> getAllSimsGrade(int pageNo, int pageSize) {
		String sql = "from SimsGrade";
		Query query = HibernateSessionFactory.getSession().createQuery(sql);
		query.setFirstResult((pageNo - 1) * pageSize);
		query.setMaxResults(pageSize);
		List<SimsGrade> list = query.list();
		HibernateSessionFactory.closeSession();
		return list;
	}

	@Override
	public long getRowCount() {
		String hql = "select count(id) from SimsGrade";
		long count = 0;
		Query query = HibernateSessionFactory.getSession().createQuery(hql);
		count = (Long) query.uniqueResult();
		HibernateSessionFactory.closeSession();
		return count;
	}

	@Override
	public long getRowCountByGradeName(String gradeName) {
		String hql = "select count(gradeName) from SimsGrade s where s.gradeName=?";
		Query query = HibernateSessionFactory.getSession().createQuery(hql);
		query.setParameter(0, gradeName);
		long count = (Long) query.uniqueResult();
		HibernateSessionFactory.closeSession();
		return count;
	}

	@Override
	public long createGrade(SimsGrade grade) {
		Long id = 0L;
		// 打开Session
		Session session = HibernateSessionFactory.getSession();
		// 开始一个事务
		Transaction trans = session.beginTransaction();
		try {
			// 持久化操作
			session.save(grade);
			// 提交事务
			trans.commit();
			id = grade.getId();
		} catch (Exception e) {
			System.out.println("error:" + e.getMessage());
			e.printStackTrace();
			// 回滚事务
			trans.rollback();
		} finally {
			// 关闭Session
			HibernateSessionFactory.closeSession();
		}
		return id;
	}

}
